package beans;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import webservice.IWebServiceCentral;
import webservice.IWebServiceCentralService;

public class Empresa extends Cliente implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// atributos
	private String razonSocial;
	private String nombrefantasia;
	private String result;
	private String tel;

	public Empresa() {

	}

	// getters y setters
	public String getRazonSocial() {
		return razonSocial;
	}

	public void setRazonSocial(String razonSocial) {
		this.razonSocial = razonSocial;
	}

	public String getNombrefantasia() {
		return nombrefantasia;
	}

	public void setNombrefantasia(String nombrefantasia) {
		this.nombrefantasia = nombrefantasia;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public void buscarEmpresaLogueo() {

		String resultado = "";
		webservice.Empresa e = new webservice.Empresa();
		e.setUsuario(getUsuario());
		e.setClave(getClave());
		webservice.Empresa retorno = new webservice.Empresa();
		IWebServiceCentralService wss = new IWebServiceCentralService();
		IWebServiceCentral ws = wss.getIWebServiceCentralPort();
		retorno = ws.buscarEmpresaLogin(e);

		if (retorno != null) {

			if (retorno.isHabilitado() == true) {
				try {
					
					FacesContext.getCurrentInstance().getExternalContext()
							.getSessionMap().put("usuario", retorno);
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("menuEmpresa.jsf");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			} else {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("errorPage.jsf");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}

	}

	public void empresaLogout() {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(
				false);
		session.invalidate();
		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("index.jsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void altaEmpresa() {

		String[] tels;
		tels = getTel().split("\n");
		webservice.Empresa e = new webservice.Empresa();
		e.setClave(getClave());
		e.setDireccion(getDireccion());
		e.setDocumento(getDocumento());
		e.setEmail(getEmail());
		e.setNombrefantasia(getNombrefantasia());
		e.setRazonSocial(getRazonSocial());
		
		if(this.getTexto()== null)
			e.setTexto("");
		else
			e.setTexto(getTexto());
		e.setUsuario(getUsuario());
		for (int n = 0; n < tels.length; n++) {
			e.getTelefonos().add(tels[n]);
		}

		// e.getTelefonos().addAll(this.getTelefonos());
		e.setHabilitado(true);
		int retorno = 0;
		IWebServiceCentralService wss = new IWebServiceCentralService();
		IWebServiceCentral ws = wss.getIWebServiceCentralPort();
		retorno = ws.altaEmpresa(e);
		try {
			if (retorno == 1)
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("altaEmpresaOk.jsf");

			else
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("errorPage.jsf");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}
